package yxy.cherry.battle.module.affix;

import yxy.apple.util.RandomUtility;
import yxy.cherry.battle.Battle;
import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.module.affix.condition.AffixCondition;
import yxy.cherry.battle.module.record.stream.RecordStream;
import yxy.cherry.battle.module.record.stream.RecordStreamGroup;
import yxy.cherry.battle.module.selector.TargetSelector;
import yxy.cherry.battle.suppression.Suppression;
import yxy.cherry.data.bean.skill;

import java.util.Collection;

public abstract class Skill extends Affix {

	private TargetSelector targetSelector;// 目标选择器
	private int probability;

	public Skill(Fighter actor, skill skilldata, int probability, int[] params) {
		super(actor, skilldata, params);
		this.probability = probability;
	}

	public int getSkillId() {
		return getSkilldata().getId();
	}

	public int getProbability() {
		return probability;
	}

	public boolean isProb() {
		int randomNumber = RandomUtility.ppm();
		Battle.battleLogger(7,"随机基数："+probability+"随机数："+randomNumber);
		return probability >= 10000 || probability > randomNumber;
	}

	public boolean active() {
		Collection<Fighter> targets = target(actor);
		if (targets == null || targets.size() <= 0) {
			return false;
		}
		boolean action = exec(targets);
		return action;
	}

	public void setTargetSelector(TargetSelector targetSelector) {
		this.targetSelector = targetSelector;
	}

	protected Collection<Fighter> target(Fighter actor) {
		return targetSelector.selectTarget(actor);
	}

	public boolean exec(Collection<Fighter> targets) {
		RecordStream recordStream = record();
		boolean 有效 = _exec(targets);
		if (有效 && recordStream != null) {
			// 技能录像流
			recordStream.record();
		}
		return 有效;
	}

	public boolean _exec(Collection<Fighter> targets) {
		boolean 有效 = false;
		for (Fighter target : targets) {
			有效 |= _exec(getActor(), target);
		}
		return 有效;
	}

	protected boolean _exec(Fighter actor, Fighter target) {
		if (!isProb()) {
			Battle.battleLogger(7,"概率未能激发成功！");
			return false;
		}
		Battle.battleLogger(7,"概率激发成功！");
		for (AffixCondition affixCondition : conditionSet) {
			boolean cond = affixCondition.check(actor, target);
			if (!cond) {
				Battle.battleLogger(7,"条件："+affixCondition.getParam()+"没达到");
				return false;
			}
		}
		Battle.battleLogger(7,"条件均满足！");
		return exec(actor, target);
	}

	public boolean isEffectiveOne(Fighter actor) {
		if (!isProb()) {
			Battle.battleLogger(4,"概率未能激发成功！");
			return false;
		}
		Battle.battleLogger(4,"概率激发成功！");
		for (AffixCondition affixCondition : conditionSet) {
			boolean cond = affixCondition.checkOne(actor);
			if (!cond) {
				Battle.battleLogger(4,"条件："+affixCondition.getParam()+"没达到");
				return false;
			}
		}
		Battle.battleLogger(4,"条件均满足！");
		return true;
	}

	public RecordStreamGroup record() {
		return null;
	}

	abstract public boolean exec(Fighter actor, Fighter target);

	/**
	 * 判断是不是特别的攻击，例如追击，普通攻击
	 * @return
	 */
	protected boolean isSpecialAttack() {
		for(AffixContext.AffixAction action : getAffixContext().get附加行动List()){
			if (action.get行动类型().contains(AffixContext.AffixActionClass.追加攻击) || action.get行动类型().contains(AffixContext.AffixActionClass.反击)){
				return true;
			}
		}
		return false;
	}

	/**
	 * 生成克制比例
	 * @param actor
	 * @param target
	 * @return
	 */
	protected double genSuppression(Fighter actor, Fighter target){
		double supptessionPer = 0;
		boolean isForbearDetect = true;
		Collection<Suppression> suppressions = actor.get克制效果();
		for (Suppression suppression : suppressions) {
			if(isForbearDetect) {//正向克制检测
				supptessionPer += suppression.get克制效果(actor, target);
				if(supptessionPer!=0) break;
				isForbearDetect = false;
			}else{//反向被克制检测
				supptessionPer += suppression.get克制效果(target, actor);
			}
			if(supptessionPer!=0){
//					System.out.println("克制伤害比例"+克制伤害比例);
			}
		}
		return supptessionPer;
	}

}
